package io.github.source.media.bean;


import com.orhanobut.logger.Logger;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import androidx.annotation.NonNull;

/**
 * 媒体提供者实现类。
 * 该类具体实现了MediaProvider接口，用于提供媒体资源的相关操作。
 */
class MediaProviderImpl implements MediaProvider {
    private final String TAG = "Source.Media.Play.Provider";
    private MediaCategoryProvider categoryProvider = MediaCategoryProvider.create();
    private MediaDetailsProvider DetailsProvider =  MediaDetailsProvider.create();
    private MediaPlayProvider playProvider = new MediaPlayProviderImpl();
    private MediaQueryProvider queryProvider =  MediaQueryProvider.create();

    public void setCategoryProvide(MediaCategoryProvider provider) {
        this.categoryProvider = provider;
    }

    public void setPlayProvider(MediaPlayProvider provider) {
        this.playProvider = provider;
    }

    public void setMediaQueryProvider(MediaQueryProvider provider) {
        this.queryProvider = provider;
    }

    // 使用 AtomicReference 来提高并发性能
    private static final AtomicReference<MediaProviderImpl> INSTANCE = new AtomicReference<>();

    // 将构造函数设为私有，防止外部直接实例化
    private MediaProviderImpl() {
        // 初始化代码
        Logger.i("初始化 MediaProviderImpl");
    }

    // 提供一个全局的访问点来获取单例实例
    public static MediaProviderImpl get() {
        MediaProviderImpl instance = INSTANCE.get();
        if (instance == null) {
            // 使用 CAS 操作来保证线程安全性
            instance = new MediaProviderImpl();
            INSTANCE.compareAndSet(null, instance);
        }
        return instance;
    }

    @NonNull
    @Override
    public MediaResult<List<MediaCategory>> getMediaCategory() {
        try {
            Logger.t(TAG).d("开始分类: ");
            return categoryProvider.getMediaCategory();
        } catch (Exception e) {
            Logger.t(TAG).e("获取数据异常: " + e);
            return MediaResult.error(500).message("获取数据异常:" + e);
        }
    }

    @NonNull
    @Override
    public MediaResult<List<MediaBean>> getMediaList(MediaQuery query) {
        try {
            Logger.t(TAG).d("开始搜索: " + query);
            return queryProvider.getMediaList(query);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 详情
     *
     * @param query 查询选项。
     * @param bean  媒体对象。
     * @return 详情
     */
    @NonNull
    @Override
    public MediaResult<List<MediaBean>> getMediaDetails(MediaQuery query, MediaBean bean) {
        Logger.t(TAG).d("MediaQuery query=" + query);
        try {
            return DetailsProvider.getMediaDetails(query, bean);
        } catch (Exception e) {
            e.printStackTrace();
            Logger.t(TAG).e("获取数据异常:" + e);
            return MediaResult.error(500).message("获取数据异常:" + e);
        }

    }

    @NonNull
    @Override
    public MediaResult<MediaPlay> getMediaPlayback(MediaQuery query, MediaBean video) {
        try {
            MediaResult<MediaPlay> result = playProvider.getMediaPlayback(query, video);
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}
